package game.logic;



import game.controller.DeathListener;
import game.logic.block.Block;
import game.logic.block.BlockFactory;
import game.util.Game;

public class GameModel {

	
	AreaModel area;
	Block block;
	
	public void bindArea(AreaModel area){
		this.area = area;
	}
	
	public void newBlock(){
		block = BlockFactory.getSquare();
		block.setRoot_x(Game.COLNUM/2 - 1);
		if(area.checkHit(block))
			death();
	}
	
	public void initArea(){
		area.init();
		newBlock();
	}
	
	public Iterable<BlockPoint> iterableArea(){
		return area;
	}
	public Iterable<BlockPoint> iterableBlock(){
		return block;
	}

	public void blockRight(){
		block.right();
		if(area.checkHit(block))
			block.left();
	}
	
	public void blockLeft(){
		block.left();
		if(area.checkHit(block))
			block.right();
	}
	
	public void blockDown(){
		block.down();
		if(area.checkHit(block))
		{
			block.up();
			area.add(block);
			check();
			newBlock();
		}
	}
	
	public void blockRotate(){
		block.rotate();
		if(area.checkHit(block)){
			block.cancelRotate();
		}
	}
	
	public void check(){
		area.clear();
		if(area.isDeath())
			death();
	}
	
	private DeathListener deathListener;
	public void setDeathListener(DeathListener deathListener){
		this.deathListener = deathListener;
	}
	
	public void death(){
		System.out.println("Death");
		deathListener.death();
	}
	
}
